#coding:utf-8
import sys, os
import json
import re
sys.path.append(".")
import hashlib
from utils import pygtrie
from config import *

from utils.jieba_wrapper import *

def is_unicode_japanese( ch ):
    if not isinstance(ch, unicode):
        return False
    i = ord( ch ) 
    if i >= 0x30A0 and i <= 0x30FF:
        return True
    elif i >= 0x3100 and i <= 0x312F:
        return True
    return False

def is_japanese( s ):
    """ 是否日语字符串 """
    if isinstance(s, str):
        s = s.decode('utf-8', 'ignore')
    tot, num = 0, 0
    for ch in s:
        i = ord( ch ) 
        tot += 1
        if is_unicode_japanese(ch) :
            num += 1
    if num > 0 and num * 2 >= tot:
        return True
    return False

def is_unicode_hanzi(c):
    d = ord(c)
    #基本汉字
    if d >= 0x4E00 and d <= 0x9FA5:
        return True
    #补充汉字
    if d >= 0x9FA6 and d <= 0x9FCB:
        return True
    #扩展A
    if d >= 0x3400 and d <= 0x4DB5:
        return True
    #兼容汉字
    if d >= 0xF900 and d <= 0xFAD9:
        return True
    return False

def is_unicode_english(ch):
    if isinstance(ch, str):
        ch = ch.decode('utf-8', 'ignore')
    if len(ch) != 1:
        return False

    if ord(ch) >= ord(u'a') and ord(ch) <= ord(u'z'):
        return True
    if ord(ch) >= ord(u'A') and ord(ch) <= ord(u'Z'):
        return True
    return False

def is_chinese( line ):
    if isinstance( line, str):
        line = line.decode('utf-8', 'ignore')
    if not isinstance( line, unicode):
        return False
    for ch in line:
        if not is_unicode_hanzi(ch):
            return False
    return True

class LetvDataMining(object):
    """
        Letv视频绑定
    """
    def __init__(self ):
        self._dmaps = {}
        self._dmaps['一'] = '1'
        self._dmaps['二'] = '2'
        self._dmaps['三'] = '3'
        self._dmaps['四'] = '4'
        self._dmaps['五'] = '5'
        self._dmaps['六'] = '6'
        self._dmaps['七'] = '7'
        self._dmaps['八'] = '8'
        self._dmaps['九'] = '9'
        self._dmaps['十'] = '10'
        self._dmaps['十一'] = '11'
        self._dmaps['十二'] = '12'
        self._dmaps['十三'] = '13'
        self._dmaps['十四'] = '14'
        self._dmaps['十五'] = '15'
        self._dmaps['十六'] = '16'
        self._dmaps['十七'] = '17'
        self._dmaps['十八'] = '18'
        self._dmaps['十九'] = '19'
        self._dmaps['二十'] = '20'
        self._dmaps['二十一'] = '21'
        self._dmaps['二十二'] = '22'
        self._dmaps['二十三'] = '23'

        self.delwords = {}
        self.delwords['英文版'] = 1
        self.delwords['英语版'] = 1
        self.delwords['国语版'] = 1
        self.delwords['日语版'] = 1
        self.delwords['美语版'] = 1
        self.delwords['粤语版'] = 1
        self.delwords['德语版'] = 1
        self.delwords['法语版'] = 1
        self.delwords['删减版'] = 1
        self.delwords['未删减版'] = 1
        self.delwords['未删减版'] = 1
        self.delwords['无删减版'] = 1
        self.delwords['合集'] = 1
        self.delwords['全集'] = 1
        self.delwords['专辑'] = 1
        self.delwords['原创'] = 1
        self.delwords['加拿大版'] = 1
        self.delwords['菲律宾版'] = 1
        self.delwords['美国版'] = 1
        self.delwords['版'] = 1
        self.delwords['澳洲版'] = 1
        self.delwords['印度版'] = 1
        self.delwords['法国版'] = 1
        self.delwords['英国版'] = 1
        self.delwords['德国版'] = 1
        self.delwords['韩国版'] = 1
        self.delwords['日本版'] = 1
        self.delwords['印尼版'] = 1
        self.delwords['挪威版'] = 1
        self.delwords['巴西版'] = 1
        self.delwords['独家策划'] = 1
        self.delwords['视频报道'] = 1
        self.delwords['片花'] = 1
        self.delwords['花絮'] = 1
        self.delwords['预告片'] = 1
        self.delwords['剧场版'] = 1
        self.delwords['原声版'] = 1
        self.delwords['中文版'] = 1
        self.delwords['汉语版'] = 1
        self.delwords['普通话版'] = 1
        self.delwords['原声版'] = 1
        self.delwords['特别版'] = 1
        self.delwords['央视版'] = 1
        self.delwords['tv版'] = 1
        self.delwords['剪辑版'] = 1
        self.delwords['cut'] = 1
        self.delwords['原声'] = 1
        self.delwords['高清版'] = 1
        self.delwords['标清'] = 1
        self.delwords['标清版'] = 1
        self.delwords['高清'] = 1
        self.delwords['超清'] = 1
        self.delwords['超清版'] = 1
        self.delwords['最新'] = 1
        self.delwords['最新版'] = 1
        self.delwords['移植版版'] = 1
        self.delwords['译制版'] = 1
        self.delwords['译制'] = 1
        self.delwords['dvd版'] = 1
        self.delwords['1080p'] = 1
        self.delwords['ova版'] = 1
        self.delwords['dvd'] = 1
        self.delwords['ova'] = 1
        self.delwords['分集版'] = 1
        self.delwords['深夜版'] = 1
        self.delwords['精华版'] = 1
        self.delwords['精编版'] = 1
        self.delwords['英版'] = 1
        self.delwords['日版'] = 1
        self.delwords['韩版'] = 1
        self.delwords['美版'] = 1
        self.delwords['sp'] = 1
        self.delwords['hd'] = 1
        self.delwords['“'] = 1
        self.delwords['”'] = 1
        self.delwords['！'] = 1
        self.delwords['。'] = 1
        self.delwords['上集'] = 1
        self.delwords['下集'] = 1
        self.delwords['上部'] = 1
        self.delwords['下部'] = 1
        self.delwords['日影'] = 1
        self.delwords['韩剧'] = 1
        self.delwords['美影'] = 1
        self.delwords['美剧'] = 1
        self.delwords['日剧'] = 1
        self.delwords['国产剧'] = 1
        self.delwords['国语'] = 1
        self.delwords['大陆剧'] = 1
        self.delwords['连续剧'] = 1
        self.delwords['电视剧'] = 1
        self.delwords['内地剧'] = 1
        self.delwords['ii'] = 1
        self.delwords['iv'] = 1
        self.delwords['iii'] = 1
        self.delwords['s1'] = 1
        self.delwords['s2'] = 1
        self.delwords['s3'] = 1
        self.delwords['s4'] = 1
        self.delwords['s5'] = 1
        self.delwords['s6'] = 1
        self.delwords['s7'] = 1
        self.delwords['s8'] = 1
        self.delwords['s9'] = 1
        self.delwords['首映礼'] = 1
        self.delwords['片段'] = 1
        self.delwords['原版'] = 1
        self.delwords['完整版'] = 1
        self.delwords['精简版'] = 1
        self.delwords['卫视版'] = 1
        self.delwords['重制版'] = 1
        self.delwords['英语版'] = 1
        self.delwords['先行版'] = 1
        self.delwords['大陆版'] = 1
        self.delwords['蓝光版'] = 1
        self.delwords['集体版'] = 1
        self.delwords['先行版'] = 1
        self.delwords['亚洲版'] = 1
        self.delwords['中字'] = 1
        self.delwords['无字'] = 1
        self.delwords['与'] = 1
        self.delwords['和'] = 1
        self.delwords['的'] = 1

    def load_dict(self):
        self._series_map = {}
        f = open(dict_dir+'/series_map.txt', 'rb')
        for line in f:
            line = line.strip()
            if line == '': continue
            fields = line.split('\t')
            if len(fields) != 2: continue
            w1 , w2 = fields
            self._series_map[w1] = w2
        f.close()

        self._sereis_da = pygtrie.CharTrie()
        splitwords = ['-', '—', ':', '：', '之', ' ', '系列']
        f = open(dict_dir+'/series.txt', 'rb')
        for line in f:
            line = line.strip()
            if line == '': continue
            fields = line.split('\t')
            key, dst = '', ''
            if len(fields) == 1:
                key = fields[0]
                dst = fields[0]+"@@@"
            elif len(fields) >= 2:
                dst = fields[0]+"@@@"+fields[1]
                key = fields[0] + " " + fields[1]
                head = fields[0]
            if not key: continue
            if not dst: continue
            key = key.replace('之','')
            words = cutwords( key )
            newwords = []
            for item in words:
                word = item['word']
                flag = item['flag']
                word = word.strip().lower()
                if not word: continue

                if word in self.delwords: continue
                if word in splitwords: continue
                uword = word.decode('utf-8', 'ignore')
                if len(uword) == 1 and not is_unicode_hanzi( uword ) and not is_unicode_english( uword ) and not is_unicode_japanese(uword):
                   continue
                if word.isalpha() and len(newwords) > 0:
                    ch = newwords[-1].decode('utf-8', 'ignore')[-1]
                    if not is_unicode_hanzi(ch):
                        word = " " + word
                newwords.append( word )
            newline = ''.join( newwords )
            newline = newline.decode('utf-8', 'ignore')
            self._sereis_da[newline] = dst


        f.close()

    def get_series(self, name ):
        if not name:
            return None
        splitwords = ['-', '—', ':', '：', '之', ' ', '系列']
        name = name.decode('utf-8', 'ignore') 
        name = re.sub(u'第[0-9一二三四五六七八九十]+[部季辑话集]',u'', name)
        name = re.sub(u' part[ ]?[0-9]+',u'', name)
        name = re.sub(u'[0-9]{6,6}',u'', name)
        name = re.sub(u'[（\【\[(][上中下][）】\]\)]',u'', name)
        name = name.strip()
        name = name.encode('utf-8', 'ignore') 
        name = name.replace('大电影','')
        name = name.replace('No Limit','')
        name = name.replace('英文原版','')
        name = name.replace('英语版','')
        name = name.replace('日文版','')
        name = name.replace('动画版','')
        name = name.replace('电影版','')
        name = name.replace('加长版','')
        name = name.replace('精华版','')
        name = name.replace('中文字幕','')
        name = name.replace('英文字幕','')
        words = cutwords( name )
        head = []
        for item in words:
            word = item['word']
            flag = item['flag']
            word = word.lower()
            if word in self.delwords: continue
            if word in splitwords:
                continue
            if word == '': continue

            uword = word.decode('utf-8', 'ignore')
            if len(uword) == 1 and not is_unicode_hanzi( uword ) and not is_unicode_english( uword ) and not is_unicode_japanese(uword):
                continue

            #前一个词不是以汉字结尾
            if word.isalpha() and len(head) > 0:
                ch = head[-1].decode('utf-8', 'ignore')[-1]
                if not is_unicode_hanzi(ch):
                    word = " " + word
            head.append( word )
        newline = ''.join( head )
        newline = newline.decode('utf-8', 'ignore')
        newline = newline.replace(u'之', u'')
        num = len(newline)
        if num > 5:
            num = 5
        for i in range(num):
            res, val = self._sereis_da.longest_prefix( newline[i:] )
            if res is not None:
                return val
        return None
    def get_syn(self, name):
        name = name.lower()
        if name in  self._series_map:
            return self._series_map[ name ]
        return None

gseries = LetvDataMining()
gseries.load_dict()

if __name__ == '__main__':

  binder = LetvDataMining()
  binder.load_dict()
  print binder.get_series('阿狸梦之岛·明星马戏团')
  print binder.get_series('阿狸梦之岛·神秘的蛋')
  print binder.get_series('阿狸梦之岛·最高点的kiss')
  print binder.get_series('阿狸梦之岛·圣诞秘密')
  print binder.get_series('阿狸梦之岛·月亮')
  print binder.get_series('绿豆蛙上班系列')
  print binder.get_series('绿豆蛙')
  print binder.get_series('绿豆蛙公益系列')
  #print binder.get_series('')
  #binder.get_syn()
  #binder.ext_douban2()

